﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace JinfuyuanProject.Repository
{
    public class PagesRepository : IPagesRepository
    {
        private readonly JinfuyuanDbContext _db;

        public PagesRepository(JinfuyuanDbContext db)
        {
            _db = db;
        }
        public IQueryable<Pages> GetList()
        {
            try
            {
                return _db.Pages;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool Delete(Pages pages)
        {
            try
            {

                _db.Entry(pages).State = System.Data.Entity.EntityState.Deleted;
                return _db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                //记录异常日志
                return false;
            }
        }
        public Pages GetModel(int Id)
        {
            try
            {
                Pages pages = _db.Pages.Find(Id);
                return pages;
            }
            catch (Exception ex)
            {
                return null;
            }

        }

        public int Insert(Pages pages)
        {
            try
            {
                _db.Entry(pages).State = System.Data.Entity.EntityState.Added;
                _db.SaveChanges();
                return pages.Id;
            }
            catch (Exception ex)
            {
                return 0;
            }

        }

        public bool Update(Pages pages)
        {
            try
            {

                _db.Entry(pages).State = System.Data.Entity.EntityState.Modified;
                return _db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
